package com.example.actionsignin.dialog;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;

import com.example.actionsignin.R;

import java.util.Calendar;

/**
 * Created by 旭 on 2017/8/9/009.
 */

public class StartDateDialogFragment extends DialogFragment {
    private View view;

    private DatePicker datePicker;

    @Override
    public void onStart() {
        super.onStart();
        DisplayMetrics displayMetrics = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        getDialog().getWindow().setLayout((int) (displayMetrics.widthPixels * 0.9f),getDialog().getWindow().getAttributes().height);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        getDialog().requestWindowFeature(STYLE_NO_TITLE);

        view = inflater.inflate(R.layout.fragment_dialog_start_date,container,false);

        init();

        return view;
    }

    private void init() {
        datePicker = (DatePicker) view.findViewById(R.id.start_date_datePicker);

        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        final int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        datePicker.init(year,month ,day , new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                String date = year + "年" + (monthOfYear + 1) + "月" + dayOfMonth + "日";

                StartTimeDialogFragment startTimeDialogFragment = new StartTimeDialogFragment();
                startTimeDialogFragment.setCancelable(false);

                Bundle bundle = new Bundle();
                bundle.putString("date",date);
                startTimeDialogFragment.setArguments(bundle);

                startTimeDialogFragment.show(getActivity().getSupportFragmentManager(),"startTimeDialog");
                dismiss();
            }
        });
    }

}
